package com.sinosoft.jwjw.base;

import com.sinosoft.jwjw.utils.ConvertUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.ParameterizedType;
import java.util.Map;

/**
 * @author: ericliu
 * @date: 2019/2/18
 * @description:
 */
public abstract class BaseController {

    @Autowired
    private HttpServletRequest request;

    /*
     * 传入原始参数json String格式
     */
    public String oparams = "";
    /*
     * 传入参数json 处理后格式
     */
    public JSONObject params = new JSONObject();

    /*
     * 用户操作说明类json String格式
     */
    public String oauth = "";
    /*
     * 用户操作说明类json 处理后格式
     */
    public JSONObject auth = new JSONObject();


    /**
     * 分页-页码   例如，第1页，第2页 从1开始
     */
    public Integer pageIndex = 1;
    /**
     * 分页-每页条数  例如，每页20条数据
     */
    public Integer pageSize = 20;





    public void BaseController() {
        RequestParamUtils();
    }
    public void BaseController(boolean isCheckAuth) {

    }


    /**
     * 初始化信息
     * @param isCheckAuth
     */
    public void initPage(boolean isCheckAuth) {

    }

    /**
     * 解析url请求中的参数
     */
    public void RequestParamUtils() {
//       request = _request;
//        oparams = _params;

        getRequestUrlData();//获取url参数
        params.discard("params");
        params.discard("funcid");
        params.discard("auth");
        params = ConvertUtil.deepMerge(params, ConvertUtil.toJSONObject(oparams));//form表单params参数里的参数，覆盖url里的参数
        auth = ConvertUtil.toJSONObject(oauth);
        this.pageIndex = ConvertUtil.toInt(params.get("pageIndex"), 1);
        this.pageSize = ConvertUtil.toInt(params.get("pageSize"), pageSize);

        System.out.println("=============系统请求原始参数=============");
        System.out.println(oparams);
        System.out.println("=============系统请求params参数=============");
        System.out.println(params.toString());
    }

    public void getRequestUrlData() {
        //初始化参数
        oauth = "";
        auth = new JSONObject();
        oparams = "";
        params = new JSONObject();
        pageIndex = 1;
        pageSize = 10;

        Map<String, String[]> map_url = request.getParameterMap();
        for (String key : map_url.keySet()) {
            String[] temp = (String[]) map_url.get(key);

            if ("auth".equals(key)) {
                oauth = temp[temp.length - 1];
            } else if ("params".equals(key)) {
                oparams = temp[temp.length - 1];
                if (oparams == null || "".equals(oparams)) {
                    oparams = "{}";
                }

                params.put(key, temp[temp.length - 1]);

            }else{
                params.put(key, temp[temp.length - 1]);
            }
        }
    }


    /**
     * 动态转换T 为对象
     * @param json
     * @param <T>
     * @return
     */
    public <T> T toObject(String json) {
        Class<T> clazz = (Class<T>) ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        JSONObject jsonObject = JSONObject.fromObject(json);
        return (T)JSONObject.toBean(jsonObject,clazz);

    }




}
